<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/7/16
 * Time: 10:20
 */
namespace app\repository;

/**
 * Class Dispatch
 * @package app\repository
 */
class Dispatch extends Repository
{

    /**
     * 创建团队
     * @param $did 任务id
     * @param $aids 团队IDS
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function team($did,$aids){
        $aids = explode(',',$aids);
        $det = [];
        $ids = [];
        foreach ($aids as $v){
            if($v>0){
                $det[] = [
                    'dispatch_id'=>$did,
                    'admin_id'=>$v,
                    'is_lead'=>0
                ];
                $ids[] = $v;
            }
        }
        $det[0]['is_lead'] = 1;



        $row = db('dispatch_at')->insertAll($det);

        db('admin')->where('admin_id','in',$ids)->update(['is_at'=>1]);

        db('dispatch')->where('id',$did)->update(['state_id'=>25]);

        $info = db('dispatch')->where('id',$did)->find();
        //发送推送消息
        $data = [
            'first'=>[
              'value'=>"您好！该工单分配给您，请你尽快处理！", 'color'=>'#000'
            ],
            "keyword1"=>["value"=>$info['number'], "color"=>"#173177"],
            "keyword2"=>["value"=>date('Y-m-d H:i:s'), "color"=>"#173177"],
            "remark"=>["value"=>"点击查看详情", "color"=>"#173177"],
        ];
        PushMess::sendTemplate($aids,$data,'NBeGDf2utQuF3NOMUJC1-uFZEINpeZu6PaasF_4_22A',getUrl().url('wchat/Otherengin/getUI').'?id='.$did);

        if($row){

            $input = [];
            $input['icon']        = "/public/static/wx/img/progress-list/t1.png";
            $input['admin_id']    = session('aid');
            $input['c_time']      = date('Y-m-d H:i:s',time());
            $input['dispatch_id'] = $did;
            $input['content']     = "已完成团队组建";
            $input['step']        = 2;

            db('dispatch_flow')->insert($input);

            return ['code'=>1,'msg'=>'创建团队成功','data'=>$row];
        }

        return ['code'=>0,'msg'=>'创建团队失败','data'=>$row];
    }

    /**
     * 添加人员
     * @param $did
     * @param $aids
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function addTeam($did,$aids,$url = 'wchat/Otherengin/getUI',$pid = false){
        $aids = explode(',',$aids);
        $aids = array_filter($aids);
        $det = [];
        $ids = [];
        foreach ($aids as $v){
            if($v>0){
                $dei = [
                    'dispatch_id'=>$did,
                    'admin_id'=>$v
                ];
                if($pid){
                    $dei['apid'] = $pid;
                }
                $det[] = $dei;
                $ids[] = $v;
            }
        }

        $row = db('dispatch_at')->insertAll($det);

        db('admin')->where('admin_id','in',$ids)->update(['is_at'=>1]);


        //发送推送消息
        $info = db('dispatch')->where('id',$did)->find();

        $data = [
            'first'=>[
                'value'=>"您好！该工单分配给您，请你尽快处理！", 'color'=>'#000'
            ],
            "keyword1"=>["value"=>$info['number'], "color"=>"#173177"],
            "keyword2"=>["value"=>date('Y-m-d H:i:s'), "color"=>"#173177"],
            "remark"=>["value"=>"点击查看详情", "color"=>"#173177"],
        ];
        PushMess::sendTemplate($aids,$data,'NBeGDf2utQuF3NOMUJC1-uFZEINpeZu6PaasF_4_22A',getUrl().url($url).'?id='.$did);

        if($row)
            return ['code'=>1,'msg'=>'添加队员成功','data'=>$row];
        return ['code'=>0,'msg'=>'添加队员失败','data'=>$row];
    }

    /**
     * 移除团队人员
     * @param $did
     * @param $aid
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function remove($did,$aid){
        $row = db('dispatch_at')->where(['dispatch_id'=>$did,'admin_id'=>$aid])->delete();
        if($row){
            db('admin')->where('admin_id',$aid)->update(['is_at'=>0]);


            //这里发送推送消息
            $info = db('dispatch')->where('id',$did)->find();

            $data = [
                'first'=>[
                    'value'=>"您好！您有工单被取消！", 'color'=>'#000'
                ],
                "keyword1"=>["value"=>$info['works_id'], "color"=>"#173177"],
                "keyword2"=>["value"=>'工单取消服务', "color"=>"#173177"],
                "keyword3"=>["value"=>'更换人员', "color"=>"#173177"],
                "remark"=>["value"=>"消息通知!", "color"=>"#173177"],
            ];
            PushMess::sendTemplate([$aid],$data,'JhdGJDM4NMSZTAR5ToJo3jmhITRCMyCadJUebgaPMEw','');

            return ['code'=>1,'msg'=>'移除队员成功','data'=>$row];
        }
        return ['code'=>0,'msg'=>'移除队员失败','data'=>$row];
    }

    /**
     * 设置领队
     * @param $did
     * @param $aid
     * @return array
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function lead($did,$aid){
        db('dispatch_at')->where(['dispatch_id'=>$did])->update(['is_lead'=>0]);
        $row = db('dispatch_at')->where(['dispatch_id'=>$did,'admin_id'=>$aid])->update(['is_lead'=>1]);
        if($row)
            return ['code'=>1,'msg'=>'设置成功'];
        return ['code'=>0,'msg'=>'设置失败'];
    }
}